package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.hutool.core.util.StrUtil;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.dao.*;
import io.renren.dto.AgencyinsuranceregmDTO;
import io.renren.dto.InsureDetailDto;
import io.renren.entity.*;
import io.renren.service.AgencyinsuranceregmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${comments}
 *
 * @author Sen 524227263@qq.com
 * @since 1.0.0 2024-05-28
 */
@Service
public class AgencyinsuranceregmServiceImpl extends CrudServiceImpl<AgencyinsuranceregmDao, AgencyinsuranceregmEntity, AgencyinsuranceregmDTO> implements AgencyinsuranceregmService {


    @Autowired
    private UcmlOrganizeDao ucmlOrganizeDao;
    @Autowired
    private UcmlContactDao ucmlContactDao;
    @Autowired
    private AgencyinsurancedDao agencyinsurancedDao;
    @Autowired
    private AgencyinsurancegiftdDao agencyinsurancegiftdDao;
    @Autowired
    private AgencyinsuranceregmDao agencyinsuranceregmDao;


    @Override
    public QueryWrapper<AgencyinsuranceregmEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<AgencyinsuranceregmEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public Map<String, Object> insureDetail(String traderMOID, InsureDetailDto dto) {
        Map<String, Object> result = new HashMap<>();
        AgencyinsuranceregmEntity info = baseDao.selectOne(new QueryWrapper<AgencyinsuranceregmEntity>()
                .eq("AgencyInsuranceRegMOID", dto.getAgencyInsuranceRegMoId()));
        if (info != null) {
            // 服务日期
            result.put("serviceDate", DateUtils.format(info.getBilldate(), "yyyy-MM-dd HH:mm:ss"));
            // 服务机构
            UcmlOrganizeEntity organizeEntity = ucmlOrganizeDao.selectOne(new QueryWrapper<UcmlOrganizeEntity>()
                    .select("OrgName").eq("UCML_OrganizeOID", info.getOrgFk()));
            result.put("orgName", organizeEntity != null ? organizeEntity.getOrgname() : "");
            // 接待员
            UcmlContactEntity ucmlContactEntity = ucmlContactDao.selectOne(new QueryWrapper<UcmlContactEntity>()
                    .select("PersonName").eq("UCML_CONTACTOID", info.getUcmlContactFk()));
            result.put("PersonName", ucmlContactEntity != null ? ucmlContactEntity.getPersonname() : "");
            // 险种明细
            List<Map<String, Object>> insureList = agencyinsurancedDao.selectListBtAgencyInsuranceRegMOID(dto.getAgencyInsuranceRegMoId());
            result.put("insureList", insureList);
            // 赠品
            List<Map<String, Object>> giftList = agencyinsurancegiftdDao.selectGiftList(dto.getAgencyInsuranceRegMoId());
            for (Map<String, Object> item : giftList) {
                if (item.get("BusinessQuantity") != null) {
                    item.put("BusinessQuantity", new BigDecimal(item.get("BusinessQuantity").toString()).setScale(2, RoundingMode.HALF_UP));
                }
            }
            result.put("giftList", giftList);
            // 卡券赠品
            List<Map<String, Object>> giftCardList = agencyinsuranceregmDao.selectGiftCardList(dto.getAgencyInsuranceRegMoId());
            result.put("giftCardList", giftCardList);
            result.put("BusinessAmount", new BigDecimal(info.getAmount()).add(new BigDecimal(info.getCustomrebateamount())).setScale(2, RoundingMode.HALF_UP));
            result.put("vipDiscount", new BigDecimal(info.getCustomrebateamount()).setScale(2, RoundingMode.HALF_UP));
            result.put("couponDiscount", 0);
            result.put("Amount", new BigDecimal(info.getAmount()).setScale(2, RoundingMode.HALF_UP));


            List<Map<String, Object>> mapList = agencyinsuranceregmDao.getAgencyinsuranceregmList(dto.getAgencyInsuranceRegMoId());
            if (mapList.size() > 0) {
                Map<String, Object> map = mapList.get(0);
                result.put("InsuranceTotalAmount", new BigDecimal(map.get("TotalAmount").toString()).setScale(2, RoundingMode.HALF_UP));
                result.put("vipDiscount", new BigDecimal(map.get("TotalFavorableAmount").toString()).setScale(2, RoundingMode.HALF_UP));
                result.put("couponDiscount", new BigDecimal(map.get("DeductionAmount").toString()).setScale(2, RoundingMode.HALF_UP));
                result.put("Amount", new BigDecimal(map.get("Amount").toString()).setScale(2, RoundingMode.HALF_UP));
            }
            result.put("InsuranceTotalAmount", new BigDecimal(info.getAmount()).add(new BigDecimal(info.getCustomrebateamount())).setScale(2, RoundingMode.HALF_UP));
            result.put("vipDiscount", new BigDecimal(info.getCustomrebateamount()).setScale(2, RoundingMode.HALF_UP));
            result.put("Amount", new BigDecimal(info.getAmount()).setScale(2, RoundingMode.HALF_UP));
        }
        return result;
    }
}